package com.cms.controller.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cms.cache.CacheManager;
import com.cms.common.kit.Constant;
import com.cms.common.kit.ResponseKit;
import com.cms.common.kit.StrKit;
import com.cms.common.kit.Tree;
import com.cms.common.kit.TreeBuilder;
import com.cms.common.util.pass.JsonUtil;
import com.cms.controller.BaseController;
import com.cms.model.Category;
import com.cms.plugin.db.Page;
import com.cms.validate.Rule;
import com.cms.validate.ValidForm;


@Controller
@RequestMapping(value = "/admin/category")
public class CategoryController extends BaseController{

	private static final String LIST_PAGE="admin/category/category_list";
	private static final String ADD_PAGE="admin/category/category_add";
	
	@RequestMapping(value = "/index")
	public String index(Map<String, Object> map,String page,HttpServletRequest request) {
		Page<Category> paginate = Category.dao.paginate(toInt(page, 1), 100);
		List<Tree> buildListToTree = TreeBuilder.buildCateToTree(paginate.getList());
		map.put("cate_tree",JsonUtil.toJson(buildListToTree));
		map.put("page", paginate);
		return LIST_PAGE;
	}
	
	@RequestMapping(value = "/toAddName")
	public String toAddName(Map<String, Object> map,String seq,String pid,HttpServletRequest request) {
		map.put("seq", seq);
		map.put("pid", pid);
		return "admin/category/category_name";
	}
	
	@RequestMapping(value = "/toAddJson")
	public @ResponseBody String toAdd(Map<String, Object> map,String id,HttpServletRequest request) {
		Category w = Category.dao.findById(id);
		return JsonUtil.toJson(w.getAttrs());
	}
	
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(Map<String, Object> map,String ids,HttpServletRequest request) {
		return Category.dao.deleteByIds(ids)==true?ResponseKit.responseSuccess("操作成功!"):ResponseKit.responseError("操作失败！");
	}
	
	
	@RequestMapping(value = "/add")
	public @ResponseBody String add(Map<String, Object> map,String id,HttpServletRequest request) {
		List<Rule> rules=new ArrayList<>();
		rules.add(new Rule().setField("name").setLabel("标题").setRules("required"));
		rules.add(new Rule().setField("folder").setLabel("目录").setRules("required"));
		String errorMsg = ValidForm.validform(request, rules);
		if(!StrKit.isBlank(errorMsg)){
			return ResponseKit.responseError(errorMsg);
		}
		boolean success=false;
		Category model = getModel(Category.class);
		if(StrKit.isBlank(id)){
			success=model.save();
		}else{
			success=model.update();
		}
		return success==true?ResponseKit.responseSuccess(JsonUtil.toJson(model.getAttrs())):ResponseKit.responseError("操作失败！");
	}
	
	@RequestMapping(value = "/addEmpty")
	public @ResponseBody String addEmpty(Map<String, Object> map,String id,HttpServletRequest request) {
		List<Rule> rules=new ArrayList<>();
		rules.add(new Rule().setField("name").setLabel("标题").setRules("required"));
		rules.add(new Rule().setField("seq").setLabel("顺序").setRules("required"));
		rules.add(new Rule().setField("pid").setLabel("父标签").setRules("required"));
		String errorMsg = ValidForm.validform(request, rules);
		if(!StrKit.isBlank(errorMsg)){
			return ResponseKit.responseError(errorMsg);
		}
		boolean success=false;
		if(StrKit.isBlank(id)){
			success=getModel(Category.class).save();
		}else{
			success=getModel(Category.class).update();
		}
		return success==true?ResponseKit.responseSuccess("操作成功!"):ResponseKit.responseError("操作失败！");
	}
	
	
}
